//index.js
var app = getApp()
var host = app.globalData.host
//引入图片预加载组件
const ImgLoader = require('../../utils/img-loader/img-loader.js')
Page({
  data: {
    image: app.globalData.image,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: host + '/OperationApi/selHomeDisplay',
      header: { 'content-type': 'application/json' },
      success:function(res){
        console.log(res)
        var product = res.data.data.listOperProduct
        that.setData({
          bannerlist: res.data.data.listBanner,
          subjectlist: res.data.data.listOperSubject,
          productlist: res.data.data.listOperProduct
        })       
        product.map(item =>{
          item.listProduct.map(itempro =>{
            var productPhotoThumb = itempro.photo
            if (wx.getStorageSync(that.data.image + productPhotoThumb)) {
              console.log(111)
              return itempro
            } else {
              console.log(222 + that.data.image + productPhotoThumb)

              wx.downloadFile({
                url: that.data.image + productPhotoThumb,
                success: function (res) {
                  console.log("item.photo:" + item.photo + ",res.temp=" + res.tempFilePath)
                  wx.setStorageSync(that.data.image + productPhotoThumb, res.tempFilePath)
                }
              })
              return itempro
            }
          })
        })
      }

    })
  },
  // 从banner位跳转至精选
  toselect:function(event){
    var selectid = event.currentTarget.dataset.selectid
    wx.navigateTo({
      url: '../select/select?id='+selectid,
    })
  },
  // 跳转至专题
  tosubject: function (event){
    var subjectid = event.currentTarget.dataset.selectid
    console.log(subjectid)
    wx.navigateTo({
      url: '../subject/subject?id=' + subjectid,
    })
  },
  // 小编推荐进入推荐列表
  torecommend:function(event){
    var recomid = event.currentTarget.dataset.recomid
    console.log(recomid)
    wx.navigateTo({
      url: '../recommendlist/recommendlist?id=' + recomid,
    })
  },
  // 跳转至商品详情页
  towed:function(event){
    var wedid = event.currentTarget.dataset.wedid
    wx.navigateTo({
      url: '../wedproductdetail/wedproductdetail?id=' + wedid,
    })
  }
})